package com.cangxuan.app.utils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cangxuan.app.DB;
import com.cangxuan.app.entity.Collections;
import com.cangxuan.app.entity.Goods;
import com.cangxuan.app.entity.Issuer;
import com.cangxuan.app.entity.MarketGoods;
import com.cangxuan.app.entity.Series;

import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class DBUtils {

    /**
     * 获取表数据
     * @param table
     * @return
     */
    public static Cursor getAllData(String table) {
        Cursor cursor = DB.sqLiteDatabase.query(table, null,
                null, null, null,
                null, null, null);
        return cursor;
    }

    /**
     * 删除表数据
     * @param table
     * @return
     */
    public static int deleteAllData(String table) {
        int delete = DB.sqLiteDatabase.delete(table, null, null);
        return delete;
    }

    /**
     * 添加首页藏品数据
     * @param collections
     * @return
     */
    public static void insertHomeGoodsData(List<Collections> collections) {
        ContentValues contentValues = new ContentValues();
        for (Collections c : collections) {
            contentValues.put("id", c.getId());
            contentValues.put("name", c.getCollectionName());
            contentValues.put("mintage", c.getMintage());
            contentValues.put("issuer", c.getIssuer());
            contentValues.put("image", c.getImage());
            contentValues.put("price", c.getOfferPrice());
            DB.sqLiteDatabase.insert("home_goods", null, contentValues);
        }
    }


    public static void insertMarketGoodsData(List<MarketGoods> goodsList) {
        ContentValues contentValues = new ContentValues();
        for (MarketGoods goods : goodsList) {
            contentValues.put("id", goods.getId());
            contentValues.put("image", goods.getImage());
            contentValues.put("name", goods.getName());
            contentValues.put("price", goods.getPrice());
            contentValues.put("count", goods.getCount());
            DB.sqLiteDatabase.insert("market_goods", null, contentValues);
        }
    }

    public static void insertHomeBannerData(List<String> banners){
        ContentValues contentValues = new ContentValues();
        banners.forEach(item->{
            contentValues.put("src",item);
            DB.sqLiteDatabase.insert("home_banner",null,contentValues);
        });
    }

    /**
     * 市场更新
     *
     * @return
     */
    public static List<MarketGoods> updateMarketGoodsCache() {
        List<MarketGoods> goodsList = new ArrayList<>();
        JSONObject jsonBody = new JSONObject();
        int[] a = new int[0];
        jsonBody.put("searchKey", "");
        jsonBody.put("category", 1);
        jsonBody.put("priceSort", 0);
        jsonBody.put("releaseDateSort", 0);
        jsonBody.put("issuers", a);
        jsonBody.put("series", a);
        jsonBody.put("pageIndex", 1);
        jsonBody.put("pageSize", 10);
        Log.i("参数", jsonBody.toJSONString());
        String jsonString = HttpUtils.post("/api/market/list", jsonBody.toJSONString());
        JSONObject jsonResponse = JSONObject.parseObject(jsonString);
        if (jsonResponse.getInteger("status") == HttpURLConnection.HTTP_OK) {
            JSONArray jsonRecords = jsonResponse.getJSONArray("value");
            int size = jsonRecords.size();
            for (int i = 0; i < size; i++) {
                JSONObject jsonGoods = jsonRecords.getJSONObject(i);
                MarketGoods goods = new MarketGoods(
                        jsonGoods.getInteger("id"),
                        jsonGoods.getString("image"),
                        jsonGoods.getString("name"),
                        jsonGoods.getInteger("count"),
                        jsonGoods.getFloat("price")
                );
                goodsList.add(goods);
            }
        }
        return goodsList;

    }

    /**
     * 首页藏品更新
     * @return
     */
    public static List<Collections> updateHomeGoodsCache() {
        List<Collections> collectionsList = new ArrayList<>();
        String jsonString = HttpUtils.get("/api/home/collections/1/10");
        JSONObject jsonResponse = JSONObject.parseObject(jsonString);
        if (jsonResponse.getInteger("status") == HttpURLConnection.HTTP_OK) {
            JSONArray jsonRecords = jsonResponse.getJSONArray("value");
            int size = jsonRecords.size();
            for (int i = 0; i < size; i++) {
                JSONObject jsonGoods = jsonRecords.getJSONObject(i);
                Collections collections = new Collections(
                        jsonGoods.getInteger("id"),
                        jsonGoods.getString("name"),
                        jsonGoods.getInteger("mintage"),
                        jsonGoods.getString("issuer"),
                        jsonGoods.getString("image"),
                        (float) jsonGoods.getInteger("price")
                );
                collectionsList.add(collections);
            }
        }
        return collectionsList;

    }

    /**
     * 首页更新轮播图
     */
    public static List<String> updateHomeBannerCache() {
        List<String> banners = new ArrayList<>();
        String jsonString = HttpUtils.get("/api/banner/list");
        JSONObject jsonResponse = JSONObject.parseObject(jsonString);
        if (jsonResponse.getInteger("status") == HttpURLConnection.HTTP_OK) {
            JSONArray jsonRecords = jsonResponse.getJSONArray("value");
            for (int i = 0; i < jsonRecords.size(); i++) {
                String banner = jsonRecords.getString(i);
                banners.add(banner);
            }
        }
        return banners;
    }


    /**
     * 删除并替换发行方
     */
    public static List<Issuer> updateMarketIssuer(){
        List<Issuer> list = new ArrayList<>();
        String jsonString = HttpUtils.get("/api/issuer/list");
        JSONObject jsonResponse = JSONObject.parseObject(jsonString);
        if (jsonResponse.getInteger("status") == HttpURLConnection.HTTP_OK) {
            JSONArray jsonRecords = jsonResponse.getJSONArray("value");
            int size = jsonRecords.size();
            for (int i = 0; i < size; i++) {
                JSONObject jsonGoods = jsonRecords.getJSONObject(i);
                Issuer issuer = new Issuer(
                        jsonGoods.getInteger("id"),
                        jsonGoods.getString("name")
                );
                list.add(issuer);
            }
        }
        return list;
    }

    public static void insertIssuer(List<Issuer> issuers) {
        ContentValues contentValues = new ContentValues();
        for (Issuer issuer : issuers) {
            contentValues.put("id", issuer.getId());
            contentValues.put("name", issuer.getName());
            DB.sqLiteDatabase.insert("market_issuer", null, contentValues);
        }
    }

    public static List<Series> updateMarketSeries(){
        List<Series> list = new ArrayList<>();
        String jsonString = HttpUtils.get("/api/series/list");
        JSONObject jsonResponse = JSONObject.parseObject(jsonString);
        if (jsonResponse.getInteger("status") == HttpURLConnection.HTTP_OK) {
            JSONArray jsonRecords = jsonResponse.getJSONArray("value");
            int size = jsonRecords.size();
            for (int i = 0; i < size; i++) {
                JSONObject jsonGoods = jsonRecords.getJSONObject(i);
                Series series = new Series(
                        jsonGoods.getInteger("id"),
                        jsonGoods.getString("name")
                );
                list.add(series);
            }
        }
        return list;
    }

    public static void insertSeries(List<Series> seriesList) {
        ContentValues contentValues = new ContentValues();
        for (Series series : seriesList) {
            contentValues.put("id", series.getId());
            contentValues.put("name", series.getName());
            DB.sqLiteDatabase.insert("market_series", null, contentValues);
        }
    }


}
